from __future__ import division
from components.recognizer import HandwrittenCharacterRecognizer as HCR
from components.processor import DataProcessor
import config
import os

def main():
    print "Hanwritten Character Recognizer"
    print "\nTraining . . . . .",
    r = HCR()
    d = DataProcessor(config.trainingDataPath, config.alphabets, config.dumpFolder, config.extension)
    trainingData = d.loadData()
    classIdx = len(trainingData[0]) - 1
    featureTrain, trueLabelsTrain = r.splitDataIntoFeaturesAndClasses(trainingData, classIdx)
    numIter = r.train(trainingData, classIdx)
    print 'done.'

    more = 'y'
    while more == 'y':
        imageFile = raw_input("\nEnter path of the image file: ")
        if os.path.exists(imageFile):
            character = r.recognizeImage(imageFile)
            print "The character is probably:", character
        else:
            print "Image file does not exist or could not be opened."
        more = raw_input("\nMore?<y/n>: ")

if __name__ == '__main__':
    main()